package edu.cs335pl.questionnaire.controller;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.draw.LineSeparator;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
import edu.cs335pl.questionnaire.pojo.ServerResponse;
import edu.cs335pl.questionnaire.pojo.entity.*;
import edu.cs335pl.questionnaire.service.UserService;
import edu.cs335pl.questionnaire.util.JWTutils;
import edu.cs335pl.questionnaire.util.ReportDemo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public ServerResponse login(@RequestBody Login login, HttpServletResponse response){
        return userService.login(login,response);
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public ServerResponse register(@ApiParam("account password company") @RequestBody Login login){
        return userService.saveUser(login) > 0 ? ServerResponse.ofSuccess("注册成功！") : ServerResponse.ofError("注册失败！");
    }

    @ApiOperation("提取公司信息")
    @GetMapping("/fetch/{account}")
    public ServerResponse fetch(@ApiParam("account") @PathVariable("account") String account){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",account);
        return ServerResponse.ofSuccess(200,"提取信息成功",userService.list(queryWrapper));
    }

    @ApiOperation("生成结果")
    @GetMapping("/generate/{account}")
    public ResponseEntity<FileSystemResource> generate(@ApiParam("account") @PathVariable("account") String account) throws DocumentException, FileNotFoundException {
        User user = userService.find(account);
        //一般此时的帐号不可能会不存在于数据库中
        if(user==null){
            return null;
        }
        else {
                File file = userService.generateFile(user);
                String filename = file.getName();
                HttpHeaders headers = new HttpHeaders();
                headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
                headers.add("Content-Disposition", "attachment; filename="+filename+".pdf");
                headers.add("Pragma", "no-cache");
                headers.add("Expires", "0");
                headers.add("Last-Modified", new Date().toString());
                headers.add("ETag", String.valueOf(System.currentTimeMillis()));
                return ResponseEntity .ok() .headers(headers) .contentLength(file.length()) .contentType(MediaType.parseMediaType("application/octet-stream")) .body(new FileSystemResource(file));
        }
    }

    @ApiOperation("提交/更新问卷结果")
    @PostMapping("/submit")
    public ServerResponse submit(@RequestBody Questionnaire questionnaire){
       return userService.submit(questionnaire) > 0 ? ServerResponse.ofSuccess("Submission is successfully done.") : ServerResponse.ofError("Submission fails.");
    }

    @ApiOperation("修改密码")
    @PostMapping("/editPassword")
    public ServerResponse editPassword(@RequestBody Login login){
        return userService.editPassword(login) > 0 ? ServerResponse.ofSuccess("Password is successfully edited.") : ServerResponse.ofError("Information edition fails.");
    }

    @ApiOperation("修改信息")
    @PostMapping("/editInf")
    public ServerResponse editInf(@RequestBody CorporateInformation corporateInformation){
        return userService.updateInfo(corporateInformation) > 0 ? ServerResponse.ofSuccess("Information is successfully edited.") : ServerResponse.ofError("Information edition fails.");
    }

    @ApiOperation("修改信息")
    @PostMapping("/editInf2")
    public ServerResponse editInf1(@RequestBody CorporateInformation2 corporateInformation){
        return userService.updateInfo2(corporateInformation) > 0 ? ServerResponse.ofSuccess("Information is successfully edited.") : ServerResponse.ofError("Information edition fails.");
    }

    @ApiOperation("获取结果")
    @GetMapping("/getReport/{account}")
    public ServerResponse getReport(@ApiParam("account") @PathVariable("account") String account){
        return ServerResponse.ofSuccess("获取成功！", userService.getReport(account));
    }
}
